import { defineConfig, loadEnv } from "vite";
import vue from "@vitejs/plugin-vue";

const API_PREFIXES = [
  "/api",
  "/auth",
  "/trade",
  "/system",
  "/buyer",
  "/seller",
  "/notify"
];

export default defineConfig(({ mode }) => {
  const env = loadEnv(mode, process.cwd(), "");
  const proxyTarget = env.VITE_PROXY_TARGET || "http://127.0.0.1:8000";

  const proxy = API_PREFIXES.reduce((acc, prefix) => {
    const config = {
      target: proxyTarget,
      changeOrigin: true,
      secure: false
    };

    if (prefix === "/api") {
      config.rewrite = path => path.replace(/^\/api/, "");
    }

    acc[prefix] = config;
    return acc;
  }, {});

  return {
    plugins: [vue()],
    server: {
      host: "0.0.0.0",
      port: 5174,
      proxy
    }
  };
});
